var GrailsError = function(field, defaultmessage, code){
    this.field = field;
    this.defaultmessage = defaultmessage;
    this.code = code;

    this.ConstructHtmlError = function(){
        var error = new YAHOO.util.Element(document.createElement('div'));
        error.addClass("error");
        error.appendChild(document.createTextNode(this.defaultmessage));
    }
};

var ErrorManager = function(xmlDoc){
    var element = xmlDoc.getElementsByTagName("list")[0];

    this.errors = [];
    
    this.GetElementValue = function(element){
        return element.firstChild.nodeValue;
    }
    
    this.AddError = function(error){
        this.errors.push(error);
    }

    this.ConstructHtmlError = function(){
        var errorsHTML = new YAHOO.util.Element(document.createElement('div'));
        errorsHTML.addClass("errors");
        for (i=0;i<this.errors.length;i++){
            var error = new YAHOO.util.Element(document.createElement('div'));
            error.addClass("error");
            error.appendChild(document.createTextNode(this.errors[i].defaultmessage));
            errorsHTML.appendChild(error);
        }
        return errorsHTML;
    }

    this.ShowDialogError = function(){
        var closeDialog = function(){this.cancel();}
        var ErrorDialog = new YAHOO.widget.SimpleDialog("ErrorDialog",{
            constraintoviewport: true,
            underlay: "shadow",
            x:300,
            y:180,
            buttons: [{text:'Close', handler: closeDialog}]
        });
        ErrorDialog.setBody("");
        ErrorDialog.render(document.body);

        new YAHOO.util.Element(ErrorDialog.body).appendChild(this.ConstructHtmlError());        
        ErrorDialog.show();
    }

    for (i=0;i<element.childNodes.length;i++){
        var fielderror = element.childNodes[i];
        var data = this.GetElementValue(fielderror.childNodes[1]);
        var defaultmessage = this.GetElementValue(fielderror.childNodes[2]);
        var code = this.GetElementValue(fielderror.childNodes[7]);
        this.AddError(new GrailsError(data, defaultmessage, code));
    }
};
